package edu.chl.grupp11.aegas.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Observer;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import edu.chl.grupp11.aegas.model.AbstractConfig;
import edu.chl.grupp11.aegas.model.ConfigFactory;
import edu.chl.grupp11.aegas.utils.Observable;
import edu.chl.grupp11.aegas.utils.ObservableAdapter;

public class FileView extends JPanel implements ActionListener, Observer,
		Observable<FileView, String> {
	private static final long serialVersionUID = 1L;

	private static String optimizeString(String fileName) {
		if (fileName.length() >= 40) {
			fileName = fileName.substring(0, 40) + "...";
		}
		return fileName;
	}

	private JLabel nameLabel;
	private JButton removeButton;
	private File file;
	private JPopupMenu pop;
	private JMenuItem rename;
	private JMenuItem delete;
	private boolean isSelected;
	private ResourceBundle bundle;
	private AbstractConfig config = ConfigFactory.getSharedConfig();

	private ObservableAdapter<FileView, String> observableAdapter;

	public FileView(File file) {
		observableAdapter = new ObservableAdapter<FileView, String>(this);
		bundle = ResourceBundle
				.getBundle("edu/chl/grupp11/aegas/recources/bundles/"
						+ "English");
		rename = new JMenuItem();
		rename.setActionCommand("RENAME");
		delete = new JMenuItem();
		rename.addActionListener(this);
		delete.addActionListener(this);
		pop = new JPopupMenu();
		pop.add(rename);
		pop.add(delete);
		nameLabel = new JLabel(optimizeString(file.getName()));
		nameLabel.setToolTipText(file.getName());
		setLayout(new BorderLayout());
		add(nameLabel, BorderLayout.WEST);
		setPreferredSize(new Dimension(300, 30));
		nameLabel.addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent me) {
				if (me.getButton() == 1) {
					click();
				} else if (me.getButton() == 3) {
					pop.show(me.getComponent(), me.getX(), me.getY());
					nameLabel.setComponentPopupMenu(pop);
				}
			}
		});
		this.addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent me) {
				if (me.getButton() == 1) {
					click();
				} else if (me.getButton() == 3) {
					pop.show(me.getComponent(), me.getX(), me.getY());
					nameLabel.setComponentPopupMenu(pop);
				}
			}
		});
		this.file = file;
		removeButton = new JButton();
		removeButton.setText("Delete");
		removeButton.addActionListener(this);
		delete.setActionCommand("DELETE");
		removeButton.setActionCommand("DELETE");
		add(removeButton, BorderLayout.EAST);
		this.setBackground(new Color(219, 219, 220));
		updateText();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		observableAdapter.setChanged();
		notifyObservers(e.getActionCommand());
	}

	@Override
	public boolean addObserver(
			edu.chl.grupp11.aegas.utils.Observer<FileView, String> l) {
		return observableAdapter.addObserver(l);
	}

	private void click() {
		observableAdapter.setChanged();
		notifyObservers("CLICK");
	}

	@Override
	public boolean deleteObserver(
			edu.chl.grupp11.aegas.utils.Observer<FileView, String> l) {
		return observableAdapter.deleteObserver(l);
	}

	@Override
	public void deleteObservers() {
		observableAdapter.deleteObservers();
	}

	public File getFile() {
		return file;
	}

	public String getFilename() {
		return file.getName();
	}

	public String getFileType() {
		return getFilename().substring(getFilename().lastIndexOf('.') + 1);
	}

	public String getPath() {
		return file.getAbsolutePath();
	}

	@Override
	public boolean hasChanged() {
		return observableAdapter.hasChanged();
	}

	public boolean isSelected() {
		return isSelected;
	}

	@Override
	public void notifyObservers(String s) {
		observableAdapter.notifyObservers(s);
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
		if (isSelected) {
			setBackground(Color.LIGHT_GRAY);
		} else {
			setBackground(new Color(219, 219, 220));
		}
	}

	@Override
	public void update(java.util.Observable o, Object arg) {
		bundle = config.getResourceBundle();
		updateText();
	}

	public void updateText() {
		bundle = config.getResourceBundle();
		delete.setText(bundle.getString("delete"));
		rename.setText(bundle.getString("rename"));
		removeButton.setText(bundle.getString("delete"));
	}
}
